package com.m3958.logcollector.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Strings;
import com.m3958.logcollector.domain.Person;
import com.m3958.logcollector.exception.SignupException;
import com.m3958.logcollector.passwordvalidator.PasswordValidator;
import com.m3958.logcollector.passwordvalidator.PasswordValidatorConfig;
import com.m3958.logcollector.passwordvalidator.PasswordValidatorException;
import com.m3958.logcollector.userdetail.PersonManager;
import com.m3958.logcollector.util.common.CommonPattern;
import com.m3958.logcollector.vo.PersonVo;
import com.m3958.logcollector.vo.SignupVo;
import com.m3958.logcollector.vo.controllresult.SignupResult;
import com.m3958.logcollector.vo.controllresult.SignupResultMessage;

@Controller
public class SignupController {
    
    private static final Logger logger = LoggerFactory.getLogger(SignupController.class);

    @Autowired
    private PersonManager personManager;
    
    
    @Autowired
    private PasswordValidatorConfig passwordValidatorConfig;


    @Autowired
    private PasswordValidator passwordValidator;
    
    
    @ExceptionHandler(Exception.class)
    public ResponseEntity<SignupResult> handleIOException(Exception ex, HttpServletRequest request) {
        SignupResult sr;
        
        if (ex instanceof SignupException) {
            logger.error(ex.getMessage());
            sr = new SignupResult(((SignupException) ex).getLrMessage());
        } else if (ex instanceof PasswordValidatorException) {
            logger.error(ex.getMessage());
            sr = new SignupResult("PASSWORDVALIDATOR_" + ex.getMessage());
        } else {
            logger.error(ex.getMessage());
            sr = new SignupResult(SignupResultMessage.UNKNOWN);
        }
        return ResponseEntity.ok(sr);
    }
    

    @RequestMapping(value = "/login/signup", method = RequestMethod.POST)
    public ResponseEntity<SignupResult> singup(HttpServletRequest request, @RequestBody SignupVo signupVo) throws SignupException, PasswordValidatorException {
        
        String name = signupVo.getName();
        String email = signupVo.getEmail();
        String mobile = signupVo.getMobile();
                
        String password = signupVo.getPassword();

        if (!CommonPattern.EMAIL_PTN.matcher(email).matches()) {
            throw new SignupException(SignupResultMessage.INVALID_EMAIL);
        }

        if (!Strings.isNullOrEmpty(mobile)) {
            if (!CommonPattern.CHINA_MOBILE.matcher(mobile).matches()) {
                throw new SignupException(SignupResultMessage.INVALID_MOBILE);
            }
        }
        
        if (name.length() < passwordValidatorConfig.getMinNameLength()) {
            throw new SignupException(SignupResultMessage.SHORT_NAME);
        }

        if (!CommonPattern.USER_NAME.matcher(name).matches() || CommonPattern.DUPLICATED_DOT.matcher(name).matches()) {
            throw new SignupException(SignupResultMessage.INVALID_NAME);
        }

        passwordValidator.validate(password);

        Person person = Person.newValidPerson();
        person.setName(name);
        person.setEmail(email);
        person.setMobile(mobile);
        person.setPassword(password);

        personManager.createUser(new PersonVo(person));

        return ResponseEntity.ok(new SignupResult());
    }
}
